package com.zt.bus.line.controller;

import com.zt.bus.constant.BusConstant;
import com.zt.bus.line.model.Refund;
import com.zt.bus.line.model.RefundDetail;
import com.zt.bus.line.service.RefundService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/refund")
public class RefundController {

    @Autowired
    private RefundService refundService;

    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(refundService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        refundService.add(getRefund(reqBean), getRefundDetailList(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        refundService.update(getRefund(reqBean), getRefundDetailList(reqBean), getDeleteRefundDetailList(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/deleteDetail")
    @ResponseBody
    public JSONObject deleteDetail(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        refundService.deleteDetail(reqBean.getInfo("refundDetailId"));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        refundService.delete(reqBean.getInfo("refundId"), reqBean.getUserId());
        return Response.responseSuccess();
    }

    /**
     * 从表单获取对象
     * 
     * @param reqBean
     * @return
     */
    private Refund getRefund(ReqBean reqBean) {
        Refund refund = new Refund();
        refund.setId(reqBean.getInfo("refundId"));
        refund.setName(reqBean.getInfo("refundName"));
        refund.setType(reqBean.getInfo("refundType"));
        refund.setStatus(reqBean.getInfo("refundStatus"));
        refund.setMinimumFee(reqBean.getInfoDouble("minimumFee"));
        refund.setOperator(reqBean.getUserId());
        refund.setRemark(reqBean.getInfo("remark"));
        return refund;
    }

    private List<RefundDetail> getRefundDetailList(ReqBean reqBean) {
        String detailListStr = reqBean.getInfo("refundDetails");
        String refundType = reqBean.getInfo("refundType");
        JSONArray array = JSONArray.fromObject(detailListStr);
        List<RefundDetail> list = new ArrayList<RefundDetail>();
        if (array != null && array.size() > 0) {
            for (int i = 0; i < array.size(); i++) {
                JSONObject json = array.getJSONObject(i);
                RefundDetail detail = new RefundDetail();
                if (BusConstant.RefundType.TIME_DISCOUNT_RATE.equals(refundType)) { // 发车前时间折扣率
                    detail.setMinutes(Integer.parseInt(json.optString("minutes")));
                    detail.setRefundRate(Double.parseDouble(json.optString("refundRate")));
                } else { // 固定费率
                    detail.setFixedFee(Double.parseDouble(json.optString("fixedFee")));
                }

                detail.setId(json.optString("refundDetailId"));
                detail.setStatus(json.getString("refundDetailStatus"));
                detail.setRefundId(reqBean.getInfo("refundId"));
                detail.setOperator(reqBean.getUserId());
                detail.setMakeTime(new Date());
                detail.setModifyTime(new Date());
                detail.setRemark(json.optString("remark"));
                list.add(detail);
            }
        }

        return list;
    }

    private JSONArray getDeleteRefundDetailList(ReqBean reqBean) {
        String deleteDetailListStr = reqBean.getInfo("deleteDetailList");
        return JSONArray.fromObject(deleteDetailListStr);
    }

}